package cn.sharp.bbs.controller.msg;

import cn.sharp.bbs.common.Result;
import cn.sharp.bbs.common.TokenUtils;
import cn.sharp.bbs.entity.BO.CurrentUser;
import cn.sharp.bbs.service.CacheService;
import cn.sharp.bbs.service.CommonService;
import cn.sharp.bbs.service.MsgPushService;
import cn.sharp.bbs.service.MsgService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息推送请求控制器
 * @author zhao yu
 * @version 0.0.1
 */
@RestController
@RequestMapping("/message")
public class MsgController {
    @Resource
    private CommonService commonService;

    @Resource
    private CacheService cacheService;

    @Resource
    private MsgPushService msgPushService;

    @Resource
    private MsgService msgService;

    /**
     * 通知订阅
     * @param token 令牌
     * @return SseEmitter 长连接的事件源(EventSource)
     */
    @RequestMapping("/subscribe")
    public SseEmitter subscribe(@RequestHeader("Token") String token) {
        CurrentUser currentUser = TokenUtils.getUserInfo(token, commonService);

        SseEmitter sseEmitter = new SseEmitter(3600_000L);
        cacheService.saveMessSseEmitter(currentUser.getU_id(), sseEmitter);
        sseEmitter.onTimeout(() -> cacheService.clearMessSseEmitter(currentUser.getU_id()));
        sseEmitter.onCompletion(() -> System.out.println("完成！！！"));
        return sseEmitter;
    }

    /**
     * 已读
     * @param type 消息类型
     * @return 成功响应
     */
    @PutMapping("/read/{type}")
    public Result read(@PathVariable("type") int type,
                       @RequestAttribute("CurrentUser") CurrentUser user){
        msgService.read(type, user.getU_id());
        return Result.success();
    }

    /**
     * 已读私信
     * @param chatRoom 聊天室id
     * @return 成功响应
     */
    @PutMapping("/read/chat/{chatRoom}")
    public Result readLetter(@PathVariable("chatRoom") Long chatRoom,
                       @RequestAttribute("CurrentUser") CurrentUser user){
        msgService.readLetter(chatRoom, user.getU_id());
        return Result.success();
    }

    /**
     * 未读数
     * @return 封装的未读数
     */
    @GetMapping("/unread")
    public Result unread(@RequestAttribute("CurrentUser") CurrentUser user) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            list.add(msgService.unread(i, user.getU_id()));
        }
        return Result.success(list);
    }

    @GetMapping("/unread2")
    public Result unread2(@RequestAttribute("CurrentUser") CurrentUser user) {
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 8; i++) {
            list.add(msgService.unread(i, user.getU_id()));
        }
        return Result.success(list);
    }

    /**
     * 聊天室未读数
     * @param chatRoom 聊天室id
     * @param user 当前用户
     */
    @GetMapping("/chatroom/unread/{chatRoom}")
    public Result unreadLetter(@PathVariable("chatRoom") Long chatRoom,
                               @RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.unreadLetter(chatRoom, user.getU_id()));
    }

    /**
     * 获得点赞通知列表
     * @param user 当前用户
     */
    @GetMapping("/like")
    public Result likeMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.likeMsg(user.getU_id()));
    }

    /**
     * 获得收藏通知列表
     * @param user 当前用户
     */
    @GetMapping("/collect")
    public Result collectMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.collectMsg(user.getU_id()));
    }

    /**
     * 获得关注通知列表
     * @param user 当前用户
     */
    @GetMapping("/follow")
    public Result followMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.followMsg(user.getU_id()));
    }

    /**
     * 获得帖子审核通知列表
     * @param user 当前用户
     */
    @GetMapping("/post/check")
    public Result postCheckMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.postCheckMsg(user.getU_id()));
    }

    /**
     * 获得评论通知列表
     * @param user 当前用户
     */
    @GetMapping("/comment")
    public Result commentMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.commentMsg(user.getU_id()));
    }

    /**
     * 获得回复通知列表
     * @param user 当前用户
     */
    @GetMapping("/reply")
    public Result replyMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.replyMsg(user.getU_id()));
    }

    /**
     * 获得举报结果通知列表
     * @param user 当前用户
     */
    @GetMapping("/report")
    public Result reportMsg(@RequestAttribute("CurrentUser") CurrentUser user) {
        return Result.success(msgService.reportMsg(user.getU_id()));
    }


    /**
     * 测试实时推送
     */
    @RequestMapping("/testPush/{id}")
    public Result testMess(@PathVariable("id") Long id) {
        try{
            msgPushService.testPush(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "error!!!");
        }
        return Result.success("success!");
    }
}